package com.tcfvic.client.panels.widgets.widgetcontents;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.tcfvic.client.panels.widgets.TCFWidgetContent;
import com.tcfvic.client.pojos.Person;
import com.tcfvic.client.tools.CarerStatus;
import com.tcfvic.client.tools.Tools;

public class PersonBackgroundTab extends AbstractDataEntryWidgets implements
		TCFWidgetContent {

	private TextBox langaugeHome = new TextBox();
	private TextBox languageWork = new TextBox();
	private ListBox numChildren = new ListBox();
	private ListBox ageYoungest = new ListBox();
//	private ListBox carerStatus = new ListBox();
	private TextBox driversLicense = new TextBox();
	private TextBox timeOffAfterRetrench = new TextBox();
//	private ListBox injurySickReason = new ListBox();
//	private ListBox natureSickReason = new ListBox();
//	private ListBox lbSecLang = new ListBox();
//	private ListBox lbThirdLang = new ListBox();
//	private ListBox lbPostSecHigh = new ListBox();
//	private ListBox lbPostSecCountry = new ListBox();


	@Override
	public Widget getContent() {
		// Create a table to layout the form options
		FlexTable ft = new FlexTable();
		ft.setCellSpacing(4);
		ft.setWidth("100%");



		
//		lbSecLang = Tools.getLanguageList();
		ft.setHTML(3, 0, "Second language");
//		ft.setWidget(3, 1, lbSecLang);
//		lbThirdLang = Tools.getLanguageList();
		ft.setHTML(4, 0, "Third language");
//		ft.setWidget(4, 1, lbThirdLang);



//		lbPostSecHigh = getPostSecLevel();
//		ft.setHTML(7, 0, "Highest level");
//		ft.setWidget(7, 1, lbPostSecHigh);
//		lbPostSecCountry = Tools.getCountryList();
//		ft.setHTML(8, 0, "Country");
//		ft.setWidget(8, 1, lbPostSecCountry);


		
//		injurySickReason = Tools.getYesNoList();
		ft.setHTML(0, 2, "Injury/illness affecting ability to work");
//		ft.setWidget(0, 3, injurySickReason);
		
//		natureSickReason = Tools.getNatureOfInjuryList();
//		ft.setHTML(1, 2, "Nature of injury/illness");
//		ft.setWidget(1, 3, natureSickReason);
//		carerStatus = getCarerStatus();
//		ft.setHTML(2, 2, "Carer status");
//		ft.setWidget(2, 3, carerStatus);

//		ft.setHTML(3, 2, "Number children");
//		numChildren = Tools.populateListBoxWithNumberRange(0, 10);
//		ft.setWidget(3, 3, numChildren);
//
//		ft.setHTML(4, 2, "Age of youngest");
//		ageYoungest = Tools.populateListBoxWithNumberRange(0, 40);
//		ft.setWidget(4, 3, ageYoungest);

		// Add some standard form options
		// ft.setHTML(row, 2, "Language at home");
		// ft.setWidget(row++, 3, langaugeHome);
		// ft.setHTML(row, 0, "Language at work");
		// ft.setWidget(row, 1, languageWork);
		// ft.setHTML(row, 0, "Highest level of school");
		// ft.setWidget(row, 1, highlestLevelSchool);
		// ft.setHTML(row, 2, "Post sec qual");
		// ft.setWidget(row++, 3, postSecQual);
		// ft.setHTML(row, 0, "Drivers license");
		// ft.setWidget(row, 1, driversLicense);
		// ft.setHTML(row, 0, "Time off after retrecnch");
		// ft.setWidget(row, 1, timeOffAfterRetrench);
		//

		saveButton.getElement().setId("saveButton");
		ft.setWidget(9, 0, saveButton);

		return ft;
	}

//	private ListBox getPostSecLevel() {
//		ListBox lb = new ListBox();
//		lb.addItem("");
//		lb.addItem("Tafe");
//		lb.addItem("Bachelors");
//		lb.addItem("Masters");
//		lb.addItem("PHD");
//		return lb;
//	}

	public static ListBox getCarerStatus() {
		final ListBox dropBox = new ListBox(false);

		dropBox.addItem("");
		for (CarerStatus ftpt : CarerStatus.values()) {
			dropBox.addItem(ftpt.getDescription());
		}

		return dropBox;
	}

	@Override
	public String getName() {
		return "Background";
	}

	@Override
	public void initialise() {

	}

	@Override
	public void clear() {
		langaugeHome.setText("");
		languageWork.setText("");
		driversLicense.setText("");
		timeOffAfterRetrench.setText("");
//		lbSecLang.setSelectedIndex(0);
//		lbThirdLang.setSelectedIndex(0);
//		lbPostSecHigh.setSelectedIndex(0);
//		lbPostSecCountry.setSelectedIndex(0);

		Tools.resetList(numChildren,
				ageYoungest);

	}

	@Override
	public <T> T saveContent(T t) throws Exception {
		if (t != null && t instanceof Person) {
			Person personDetailsDTO = (Person) t;

			personDetailsDTO.setLanguagesathome(langaugeHome.getText());
			personDetailsDTO.setLanguagesatwork(languageWork.getText());
//			try {
//				personDetailsDTO.setPostSecHighLevel(lbPostSecHigh
//						.getItemText(lbPostSecHigh.getSelectedIndex()));
//			} catch (Exception e) {
//			}
//			try {
//				personDetailsDTO.setPostSecCountry(lbPostSecCountry
//						.getItemText(lbPostSecCountry.getSelectedIndex()));
//			} catch (Exception e) {
//			}
			try {
				personDetailsDTO.setNumberchildren(Integer.valueOf(numChildren
						.getItemText(numChildren.getSelectedIndex())));
			} catch (Exception e) {
			}
			try {
				personDetailsDTO.setAgeofyoungestchild(Integer
						.valueOf(ageYoungest.getItemText(ageYoungest
								.getSelectedIndex())));
			} catch (Exception e) {
			}
//			try {
//				personDetailsDTO.setSecLanguage(lbSecLang.getItemText(lbSecLang
//						.getSelectedIndex()));
//			} catch (Exception e) {
//			}
//			try {
//				personDetailsDTO.setThirdLanguage(lbThirdLang.getItemText(lbThirdLang
//						.getSelectedIndex()));
//			} catch (Exception e) {
//			}
//			try {
//				personDetailsDTO.setCarerstatus(carerStatus
//						.getItemText(carerStatus.getSelectedIndex()));
//			} catch (Exception e) {
//			}
			personDetailsDTO.setDriverslicense(driversLicense.getText());
			personDetailsDTO.setTimeoffafterretrechment(timeOffAfterRetrench
					.getText());
//			try {
//				personDetailsDTO.setInjurysicknessreason(injurySickReason
//						.getItemText(injurySickReason.getSelectedIndex()));
//			} catch (Exception e) {
//			}
//			try {
//				personDetailsDTO.setNatureOfIllness(natureSickReason
//						.getItemText(natureSickReason.getSelectedIndex()));
//			} catch (Exception e) {
//			}
		}
		return t;
	}

	@Override
	public void updateContent(Object result) {
		clear();
		if (result instanceof Person) {
			Person person = (Person) result;

			langaugeHome.setText(person.getLanguagesathome());
			languageWork.setText(person.getLanguagesatwork());
//			lbSecLang.setSelectedIndex(Tools.getSelectedIndex(lbSecLang, person
//					.getSecLanguage()));
//			lbThirdLang.setSelectedIndex(Tools.getSelectedIndex(lbThirdLang,
//					person.getThirdLanguage()));
//			lbPostSecHigh.setSelectedIndex(Tools.getSelectedIndex(
//					lbPostSecHigh, person.getPostSecHighLevel()));
//			lbPostSecCountry.setSelectedIndex(Tools.getSelectedIndex(
//					lbPostSecCountry, person.getPostSecCountry()));

			try {
				numChildren.setSelectedIndex(Tools.getSelectedIndex(
						numChildren, person.getNumberchildren().toString()));
			} catch (Exception e) {
			}
			try {
				ageYoungest
						.setSelectedIndex(Tools.getSelectedIndex(ageYoungest,
								person.getAgeofyoungestchild().toString()));
			} catch (Exception e) {
			}
			driversLicense.setText(person.getDriverslicense());
			timeOffAfterRetrench.setText(person.getTimeoffafterretrechment());
//			injurySickReason.setSelectedIndex(Tools.getSelectedIndex(
//					injurySickReason, person.getInjurysicknessreason()));
//			natureSickReason.setSelectedIndex(Tools.getSelectedIndex(
//					natureSickReason, person.getNatureOfIllness()));
//			carerStatus.setSelectedIndex(Tools.getSelectedIndex(carerStatus,
//					person.getCarerstatus()));
		}
	}

	@Override
	public void createNewRecord() {
		// TODO Auto-generated method stub
		
	}

}
